package util;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

/**
 * 数据库工具类
 */
public class DBHelper {

    //1+2
    public Connection getCon(){
        Connection con=null;
        try {
            //1.手动添加数据库驱动类：
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得数据库连接对象：
            String url = "jdbc:mysql://127.0.0.1:3306/human resource management system?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
            String username = "root";
            String password = "123456";
             con= DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    //7
    public void closeAll(Connection con,PreparedStatement ps,ResultSet rs){
      try {
          if (rs != null) {
              rs.close();
          }
          if (ps != null) {
              ps.close();
          }
          if (con != null) {
              con.close();
          }
      } catch (SQLException e) {
          e.printStackTrace();
      }
    }


    // 增删改的通用方法
    public int dtUpdate(String sql, Object... obj) {
        Connection con = getCon();
        PreparedStatement ps = null;
        int count = 0;
        try {
            ps = con.prepareStatement(sql);

            for (int i = 0; i < obj.length; i++) {
                Object param = obj[i];

                if (param == null) {
                    ps.setNull(i + 1, Types.VARCHAR);
                } else if (param instanceof String) {
                    ps.setString(i + 1, (String) param);
                } else if (param instanceof Integer) {
                    ps.setInt(i + 1, (Integer) param);
                } else if (param instanceof Double) {
                    ps.setDouble(i + 1, (Double) param);
                } else if (param instanceof Float) {
                    ps.setFloat(i + 1, (Float) param);
                } else if (param instanceof Long) {
                    ps.setLong(i + 1, (Long) param);
                } else if (param instanceof Boolean) {
                    ps.setBoolean(i + 1, (Boolean) param);
                } else if (param instanceof java.sql.Date) {
                    ps.setDate(i + 1, (java.sql.Date) param);
                } else if (param instanceof java.sql.Timestamp) {
                    ps.setTimestamp(i + 1, (java.sql.Timestamp) param);
                } else if (param instanceof java.util.Date) {
                    // 关键修复：将 java.util.Date 转换为 java.sql.Date
                    java.util.Date utilDate = (java.util.Date) param;
                    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                    ps.setDate(i + 1, sqlDate);
                } else {
                    ps.setString(i + 1, param.toString());
                }
            }

            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }
        return count;
    }

    // 通用的查询方法
    public ArrayList dtQuery(String sql, Class cla, Object... obj) {
        ArrayList list = new ArrayList();
        Connection con = getCon();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);

            // 修复参数设置：根据参数类型使用正确的set方法
            for (int i = 0; i < obj.length; i++) {
                Object param = obj[i];

                if (param == null) {
                    ps.setNull(i + 1, Types.VARCHAR);
                } else if (param instanceof String) {
                    ps.setString(i + 1, (String) param);
                } else if (param instanceof Integer) {
                    ps.setInt(i + 1, (Integer) param);
                } else if (param instanceof Double) {
                    ps.setDouble(i + 1, (Double) param);
                } else if (param instanceof Float) {
                    ps.setFloat(i + 1, (Float) param);
                } else if (param instanceof Long) {
                    ps.setLong(i + 1, (Long) param);
                } else if (param instanceof Boolean) {
                    ps.setBoolean(i + 1, (Boolean) param);
                } else if (param instanceof java.sql.Date) {
                    ps.setDate(i + 1, (java.sql.Date) param);
                } else if (param instanceof java.sql.Timestamp) {
                    ps.setTimestamp(i + 1, (java.sql.Timestamp) param);
                } else if (param instanceof java.util.Date) {
                    // 关键修复：将 java.util.Date 转换为 java.sql.Date
                    java.util.Date utilDate = (java.util.Date) param;
                    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                    ps.setDate(i + 1, sqlDate);
                } else {
                    ps.setString(i + 1, param.toString());
                }
            }

            rs = ps.executeQuery();
            while (rs.next()) {
                Object o = cla.newInstance();
                Field[] fields = cla.getDeclaredFields();
                for (Field f : fields) {
                    f.setAccessible(true);
                    f.set(o, rs.getObject(f.getName()));
                }
                list.add(o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return list;
    }
    //查询数量的方法
    public Object executeScalar(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getCon();
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    Object param = params[i];

                    if (param == null) {
                        ps.setNull(i + 1, Types.VARCHAR);
                    } else if (param instanceof String) {
                        ps.setString(i + 1, (String) param);
                    } else if (param instanceof Integer) {
                        ps.setInt(i + 1, (Integer) param);
                    } else if (param instanceof Double) {
                        ps.setDouble(i + 1, (Double) param);
                    } else if (param instanceof Float) {
                        ps.setFloat(i + 1, (Float) param);
                    } else if (param instanceof Long) {
                        ps.setLong(i + 1, (Long) param);
                    } else if (param instanceof Boolean) {
                        ps.setBoolean(i + 1, (Boolean) param);
                    } else if (param instanceof java.sql.Date) {
                        ps.setDate(i + 1, (java.sql.Date) param);
                    } else if (param instanceof java.sql.Timestamp) {
                        ps.setTimestamp(i + 1, (java.sql.Timestamp) param);
                    } else if (param instanceof java.util.Date) {
                        // 关键修复：将 java.util.Date 转换为 java.sql.Date
                        java.util.Date utilDate = (java.util.Date) param;
                        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                        ps.setDate(i + 1, sqlDate);
                    } else {
                        ps.setString(i + 1, param.toString());
                    }
                }
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getObject(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return null;
    }
}
